//
//  ZMStyle.swift
//  zhonglibang
//
//  Created by Rowling on 2021/1/5.
//

import Foundation
import UIKit

class Style {
    static let mainColorStr = "#DE3F26"
    static let mainColor = mainColorStr.zm.hex()
    
    static let moneyColorStr = "#FA2C19"
    static let moneyColor = moneyColorStr.zm.hex()
    
    static let disableColorStr = "#CCCCCC"
    static let disableColor = disableColorStr.zm.hex()
    
    static let lineColor = UIColor.zm.color(hexString: "000000", 0.1)
    
    
    // fonts
    class func FONT_Alternate_Bold(_ size : CGFloat) -> UIFont {
        if let f = UIFont.init(name: "DINAlternate-Bold", size: size) {
            return f
        } else {
            return UIFont.systemFont(ofSize: size, weight: .bold)
        }
    }
    
    //
    class func FONT_Sans_SemiBold(_ size : CGFloat) -> UIFont {
        if let f = UIFont.init(name: "GillSans-SemiBold", size: size) {
            return f
        } else {
            return UIFont.systemFont(ofSize: size, weight: .bold)
        }
    }
    
    class func FONT_Open_Sans_SemiBold(_ size : CGFloat) -> UIFont {
        if let f = UIFont.init(name: "OpenSans-SemiBold", size: size) {
            return f
        } else {
            return UIFont.systemFont(ofSize: size, weight: .bold)
        }
    }
    
}

public struct ZMStyle<Base> {
    var base : Base
    init(base : Base) {
        self.base = base
    }
}

public protocol ZMStyleCompatible {}

public extension ZMStyleCompatible {
    var style : ZMStyle<Self> {
        get {ZMStyle(base: self)}
        set {}
    }
    
    static var zm:ZMStyle<Self>.Type {
        get {ZMStyle<Self>.self}
        set {}
    }
}
extension UIView : ZMStyleCompatible{}



extension ZMStyle where Base : UIView {
    func shadowNormal() {
        base.layer.shadowRadius = 8
        base.layer.shadowOffset = CGSize(width: 0, height: 4)
        base.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.05).cgColor
        base.layer.shadowOpacity = 1
    }
    
    func snpEqualSuper() {
        base.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func snpEqualSuperCenter() {
        base.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    // 默认渐变色
    func DefaultGradientLayer() {
        var grLayer : CAGradientLayer!
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        if grLayer == nil {
            grLayer = CAGradientLayer()
            grLayer.locations = [0, 1]
            grLayer.startPoint = CGPoint(x: 1.00, y: 0.50)
            grLayer.endPoint = CGPoint(x: 0.00, y: 0.50)
            base.layer.addSublayer(grLayer)
        }
        grLayer.colors = [Style.mainColor.cgColor, Style.mainColor.cgColor]
        grLayer.frame = base.bounds
    }
    
    // 默认渐变色
    func DisableGradientLayer() {
        var grLayer : CAGradientLayer!
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        if grLayer == nil {
            grLayer = CAGradientLayer()
            grLayer.locations = [0, 1]
            grLayer.startPoint = CGPoint(x: 1.00, y: 0.50)
            grLayer.endPoint = CGPoint(x: 0.00, y: 0.50)
            base.layer.addSublayer(grLayer)
        }
        grLayer.colors = ["cccccc".zm.hex().cgColor, "cccccc".zm.hex().cgColor]
        grLayer.frame = base.bounds
    }
    
    // 默认渐变色
    func GradientLeftToRight(left:UIColor,right:UIColor) {
        var grLayer : CAGradientLayer!
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        if grLayer == nil {
            grLayer = CAGradientLayer()
            grLayer.locations = [0, 1]
            grLayer.startPoint = CGPoint(x: 0.00, y: 0.50)
            grLayer.endPoint = CGPoint(x: 1.00, y: 0.50)
            base.layer.insertSublayer(grLayer, at: 0) //.addSublayer(grLayer)
        }
        grLayer.colors = [left.cgColor, right.cgColor]
        grLayer.frame = base.bounds
    }
    
    // 默认渐变色
    func GradientLeftToRight(Top:UIColor, Bottom:UIColor, locations : [NSNumber] = [0,1]) {
        var grLayer : CAGradientLayer!
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        if grLayer == nil {
            grLayer = CAGradientLayer()
            grLayer.locations = locations
            grLayer.startPoint = CGPoint(x: 0.50, y: 0.00)
            grLayer.endPoint = CGPoint(x: 0.50, y: 1.00)
            base.layer.insertSublayer(grLayer, at: 0) //.addSublayer(grLayer)
        }
        grLayer.colors = [Top.cgColor, Bottom.cgColor]
        grLayer.frame = base.bounds
    }
    
    func Gradient(colors : [CGColor],
                  locations : [NSNumber] = [0,1],
                  startPoint : CGPoint = CGPoint(x: 0, y: 0.5),
                  endPoint : CGPoint = CGPoint(x: 1, y: 0.5)) {
        var grLayer : CAGradientLayer!
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        if grLayer == nil {
            grLayer = CAGradientLayer()
            base.layer.insertSublayer(grLayer, at: 0) //.addSublayer(grLayer)
        }
        grLayer.locations = locations
        grLayer.startPoint = startPoint
        grLayer.endPoint = endPoint
        grLayer.colors = colors
        grLayer.frame = base.bounds
    }
    
    
    
    
    // 默认渐变色
    func GradientLayerFit() {
        var grLayer : CAGradientLayer?
        if let existLayer = base.layer.sublayers?.filter({ (layer) -> Bool in
             return layer is CAGradientLayer
        }).first as? CAGradientLayer{
            grLayer = existLayer
        }
        grLayer?.frame = base.bounds
    }
}
